<template>
  <div class="course">
    <div class="course-header">
      <div class="title">{{ datas.title }}</div>
      <div class="desc">{{ datas.desc }}</div>
    </div>
    <div class="course-body">
      <el-form :model="datas.form" size="small" label-width="82px">
        <!-- 板块名称选择 -->
        <el-form-item label="标题名称">
          <el-input
            v-model="datas.form.title"
            type="text"
            placeholder="请输入标题名称"
            size="small"
            maxlength="16"
            show-word-limit
          ></el-input>
        </el-form-item>

        <!-- 课程类型选择 -->
        <el-form-item label="课程类型">
          <el-select
            v-model="datas.form.course_type"
            placeholder="选择课程类型"
            @change="handlerChangeCourseType"
          >
            <el-option
              v-for="item in courseOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>

        <!-- 排列方式选择 -->
        <el-form-item label="展示方式">
          <el-radio-group v-model="datas.form.arrange_type">
            <el-radio
              v-for="item in arrangeOptions"
              :key="item.label"
              :label="item.label"
            >
              {{ item.title }}
            </el-radio>
          </el-radio-group>
        </el-form-item>

        <!-- 课程数量选择 -->
        <el-form-item label="展示课程数">
          <el-input-number
            v-model="datas.form.number"
            :min="2"
            :max="99"
            @change="handlerChangeNumber"
          ></el-input-number>
        </el-form-item>

        <!-- 数据来源选择 -->
        <el-form-item label="课程来源">
          <el-select
            v-model="datas.form.source"
            placeholder="选择课程来源"
            @change="handlerChangeSource"
          >
            <el-option
              v-for="item in sourceOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>

        <!-- 自定义课程 -->
        <el-form-item v-if="datas.form.source ==='custom'" label="自定义课程">
          <el-button type="primary" size="mini" @click="chooseCourseVisible = true">选择课程</el-button>
        </el-form-item>
      </el-form>

      <div v-if="datas.form.source ==='custom'" class="course-footer">
        <div class="title">已选课程 {{ datas.form.source_list.length }}/{{ datas.form.number }}</div>
        <div v-show="datas.form.source_list.length > 0" class="box">
          <div v-for="item in datas.form.source_list" :key="item.value" class="item">
            <span class="text">{{ item.title }}</span>
            <i class="el-icon-close close" @click="removeSource(item.value)"></i>
          </div>
        </div>
      </div>
    </div>
    <!-- 选择课程 start-->
    <choose-course-dialog
      v-if="chooseCourseVisible"
      :visible.sync="chooseCourseVisible"
      :limit="datas.form.number"
      :list="datas.form.source_list"
      from="config"
      @submit="setCurrentItemCourse"
    ></choose-course-dialog>
    <!-- 选择课程 end-->
  </div>
</template>

<script>
import ChooseCourseDialog from '@/pages/course/components/ChooseCourseDialog'

export default {
  name: 'CourseStyle',
  components: {
    ChooseCourseDialog
  },
  props: {
    datas: {
      type: Object,
      default: () => {
      }
    }
  },
  data() {
    return {
      arrangeOptions: [
        {
          label: 1,
          title: '列表'
        },
        {
          label: 2,
          title: '宫格'
        },
        {
          label: 3,
          title: '滚动'
        }
      ],
      courseOptions: [
        {
          value: 0,
          label: '不限'
        },
        {
          value: 1,
          label: '点播'
        },
        {
          value: 2,
          label: '直播'
        },
        {
          value: 4,
          label: '班级课'
        }
      ],
      defaultOption: [
        {
          label: '最新课程',
          value: 'latest'
        },
        {
          label: '热销课程',
          value: 'hot'
        },
        {
          label: '推荐课程',
          value: 'splendid'
        },
        {
          label: '活动课程',
          value: 'event'
        },
        {
          label: '免费课程',
          value: 'free'
        }
      ],
      optionOne: [
        {
          label: '最近直播',
          value: 'lately_live'
        }
      ],
      optionTwo: [
        {
          label: '自定义',
          value: 'custom'
        }
      ],
      chooseCourseVisible: false
    }
  },
  computed: {
    sourceOptions() {
      let options = []
      switch (this.datas.form.course_type) {
          // 不限
        case 0:
          options = options.concat(this.defaultOption).concat(this.optionTwo)
          break;
          // 点播
        case 1:
          options = options.concat(this.defaultOption)
          break;
          // 直播
        case 2:
          options = options.concat(this.optionOne).concat(this.defaultOption)
          break;
          // 班级课
        case 4:
          options = options.concat(this.defaultOption)
          break;
        default:
          options = options.concat(this.defaultOption)
          break;
      }

      return options
    }
  },
  methods: {
    setCurrentItemCourse(value, course) {
      this.datas.form.source_list = course
      this.chooseCourseVisible = false
      this.setSourceIndex()
    },
    // 移除选中的资源
    removeSource(val) {
      const index = this.datas.form.source_list.findIndex(item => item.value === val)
      this.datas.form.source_list.splice(index, 1)
      this.setSourceIndex()
    },
    // 类型改变
    handlerChangeCourseType() {
      this.datas.form.source = undefined
      this.setSourceIndex()
    },
    // 数量改变
    handlerChangeNumber() {
      this.setSourceIndex()
    },
    // 数据来源改变
    handlerChangeSource() {
      this.setSourceIndex()
    },
    setSourceIndex() {
      if (this.$util.isUndef(this.datas.form.source_index)) {
        this.$set(this.datas.form, 'source_index', 0)
      }
      this.datas.form.source_index = Math.random()
    }
  }
}
</script>

<style scoped lang="scss">
.course {
  padding-top: 20px;
  padding-left: 24px;
  padding-right: 34px;

  &-header {
    padding-bottom: 10px;
    border-bottom: 1px solid #ebeef5;

    .title {
      font-size: 16px;
      font-weight: 500;
      color: #303133;
      line-height: 24px;
    }

    .desc {
      margin-top: 10px;
      font-size: 12px;
      font-weight: 400;
      color: #909399;
      line-height: 16px;
    }
  }

  &-body {
    margin-top: 20px;

    ::v-deep.el-radio {
      margin-right: 20px;

      &:last-child {
        margin-right: 0;
      }
    }
  }

  &-footer {
    margin-top: 20px;

    .title {
      font-size: 12px;
      font-weight: 400;
      color: #b7bac1;
      line-height: 16px;
    }

    .box {
      margin-top: 10px;
      background: #f7f7f7;
      padding: 0 10px;
    }

    .item {
      padding: 12px 0;
      display: flex;
      align-items: center;

      & + .item {
        border-top: 1px solid #ebeef5;
      }

      .text {
        flex: 1;
        min-width: 0;
        font-size: 14px;
        font-weight: 400;
        color: #303133;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
      }

      .close {
        font-size: 12px;
        color: #8c8c8c;
        cursor: pointer;
      }
    }
  }
}
</style>
